//
//  explainTableViewCell.m
//  Estate
//
//  Created by wangdongdong on 2016/11/15.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "explainTableViewCell.h"

@implementation explainTableViewCell

- (void)setIsEdit:(BOOL)isEdit{
    if(isEdit){
        self.text.editable=YES;
    }else{
        self.text.editable=NO;
    }
}

- (void)setTitle:(NSString *)title andPlaceHolder:(NSString *)pl andTextValue:(NSString *)value{
    self.title.font=[UIFont systemFontOfSize:16*BIZ];
    self.text.font=[UIFont systemFontOfSize:16*BIZ];
    self.title.text=title;
    self.text.text=value;
    self.text.placeholder=pl;
}

- (void)setTitleColor:(UIColor *)color1 andTextColor:(UIColor *)color2{
    self.title.textColor=color1;
    self.text.textColor=color2;
    
};

- (void)awakeFromNib {
    [super awakeFromNib];
    self.title.font=[UIFont systemFontOfSize:16*BIZ];
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    self.title.textColor=UIColorFromRGB(0x797979);
    self.text.textColor=UIColorFromRGB(0x797979);
    self.text.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    self.text.layer.cornerRadius=10.;
    self.text.delegate=self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}




//设置分割线
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect);
    //    //上分割线，
    //    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor); CGContextStrokeRect(context, CGRectMake(0, -1, rect.size.width , 1));
    //下分割线
    CGContextSetStrokeColorWithColor(context,[UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00].CGColor);
    CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width, 1));
}

#pragma mark UITextFieldDelegate


- (void)textViewDidChange:(UITextView *)textView{
    //    NSLog(@"输入框:%@",textView.text);
    //    NSRange textRange = [textView selectedRange];
    //    [textView setText:[self disable_emoji:[textView text]]];
    //    NSLog(@"经过过滤:%@",[self disable_emoji:[textView text]]);
    //    [textView setSelectedRange:textRange];
    
    //过滤掉系统表情
    //    UITextView * textView;
    //
    //    NSRange textRange = [textView selectedRange];
    //    [textView setText:[self disable_emoji:[textView text]]];
    //    [textView setSelectedRange:textRange];
    
    NSString *toBeString = textView.text;
    
    //    NSLog(@"-----toBeString  1  ----%@",toBeString);
    
    BOOL isEmoj = [self stringContainsEmoji:toBeString];
    NSString * _showStr;
    
    
    toBeString = [self disable_emoji:toBeString];
    
    
    //    NSLog(@"-----toBeString   2  -----%@",toBeString);
    
    if (isEmoj) {
        
        if ([_showStr length]) {
            
            textView.text = _showStr;
            
        }else{
            textView.text = toBeString;
        }
    }
    
    if (self.textValueChangedBlock) {
        self.textValueChangedBlock(textView.text);
    }
}


- (NSString *)disable_emoji:(NSString *)text
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    //判断加上输入的字符，是否超过界限
    
    //    NSString *string = [NSString stringWithFormat:@"%@%@", textView.text, text];
    //    if (string.length > TEXT_MAXLENGTH){
    //        return NO;
    //    }
    
    //   限制苹果系统输入法  禁止输入表情
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    return YES;
}



//判断NSString字符串是否包含emoji表情
- (BOOL)stringContainsEmoji:(NSString *)string
{
    __block BOOL returnValue =NO;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        const unichar hs = [substring characterAtIndex:0];
        // surrogate pair
        if (0xd800) {
            if (0xd800 <= hs && hs <= 0xdbff) {
                if (substring.length > 1) {
                    const unichar ls = [substring characterAtIndex:1];
                    const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                    if (0x1d000 <= uc && uc <= 0x1f77f) {
                        returnValue =YES;
                    }
                }
            }else if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                if (ls == 0x20e3) {
                    returnValue =YES;
                }
            }else {
                // non surrogate
                if (0x2100 <= hs && hs <= 0x27ff) {
                    returnValue =YES;
                }else if (0x2B05 <= hs && hs <= 0x2b07) {
                    returnValue =YES;
                }else if (0x2934 <= hs && hs <= 0x2935) {
                    returnValue =YES;
                }else if (0x3297 <= hs && hs <= 0x3299) {
                    returnValue =YES;
                }else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                    returnValue =YES;
                }
            }
        }
    }];
    return returnValue;
}

@end
